package com.googlecode.gingerman.world.geometry;

import java.util.ArrayList;

public class Rectangle extends Polygon {

	private double a,b,angle;
	
	public Rectangle(Coordinates center, double a, double b, double angle) {
		
		super(center, new ArrayList<Vector>());

		this.a=a;
		this.b=b;
		this.angle=angle;
		
		if ((double)((int)(angle/Math.PI/2))==angle/Math.PI/2) angle=0;
		if (angle<Math.PI/2) angle=Math.PI/2-angle;
		if (angle>Math.PI/2){
			int tmp=(int)(angle/Math.PI/2);
			angle=Math.abs(angle-tmp*Math.PI/2);
		}
		Vector vector;
		if (angle==0 || angle==Math.PI/2){
			vector=new Vector(this.getCenter());
			vertices.add((new Vector(new Coordinates(-a / 2, -b / 2))).add(vector));
			vertices.add((new Vector(new Coordinates(a / 2, -b / 2))).add(vector));
			vertices.add((new Vector(new Coordinates(a / 2, b / 2))).add(vector));
			vertices.add((new Vector(new Coordinates(-a / 2, b / 2))).add(vector));
		} else{
			double tmplength,tmpangle;
			Vector tmpvector;
			vector=new Vector(this.getCenter());
			tmpvector=new Vector(new Coordinates(-a / 2, -b / 2));
			tmplength=tmpvector.getLength();
			tmpangle=tmpvector.getAngle();
			vertices.add((new Vector(tmpangle+angle, tmplength).add(vector)));
			tmpvector=new Vector(new Coordinates(a / 2, -b / 2));
			tmplength=tmpvector.getLength();
			tmpangle=tmpvector.getAngle();
			vertices.add((new Vector(tmpangle+angle, tmplength).add(vector)));
			tmpvector=new Vector(new Coordinates(a / 2, b / 2));
			tmplength=tmpvector.getLength();
			tmpangle=tmpvector.getAngle();
			vertices.add((new Vector(tmpangle+angle, tmplength).add(vector)));
			tmpvector=new Vector(new Coordinates(-a / 2, b / 2));
			tmplength=tmpvector.getLength();
			tmpangle=tmpvector.getAngle();
			vertices.add((new Vector(tmpangle+angle, tmplength).add(vector)));
		}
		
	}
	
	public double getWidth(){
		return a;
	}
	
	public double getHeigth(){
		return b;
	}
	
	public double getAngle(){
		return angle;
	}
	
	public double getDiag(){
		return Math.sqrt(a*a+b*b);
	}
	
	public double getHalfDiag(){
		return Math.sqrt(a*a/4+b*b/4);
	}

}
